package com.petRescueCenter.demo.posting.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.petRescueCenter.demo.posting.dao.ReplyDao;
import com.petRescueCenter.demo.posting.entity.Reply;
import com.petRescueCenter.demo.posting.entity.dto.ReplyDTO;

@Service
public class ReplyService implements IReplyService {
	@Autowired
	private ReplyDao replyDao;

	@Override
	public Reply save(Reply entity) {
		// TODO Auto-generated method stub
		return replyDao.save(entity);
	}

	@Override
	public Reply findById(Long id) {
		// TODO Auto-generated method stub
		return replyDao.findById(id).get();
	}

	@Override
	public boolean existsById(Long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public long count() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void deleteById(Long id) {
		// TODO Auto-generated method stub
		replyDao.deleteById(id);
	}

	@Override
	public void deleteAll(Long[] ids) {
		// TODO Auto-generated method stub

	}

	@Override
	public Page<ReplyDTO> findAll(Specification<Reply> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<Reply> entityLists = replyDao.findAll(spec, pageable);
		List<ReplyDTO> dtoLists = null;
		if (entityLists != null) {
			dtoLists = new ArrayList<ReplyDTO>();
			for (Reply entity : entityLists) {
				ReplyDTO dto = new ReplyDTO();
				ReplyDTO.entityToDto(dto, entity);
				dtoLists.add(dto);
			}
		}
		return new PageImpl<ReplyDTO>(dtoLists, pageable, entityLists.getTotalElements());
	}

}
